package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.zbg.MaintenanceStaffData;
import com.ruoyi.system.domain.zbg.WarningAndExceptionData;
import com.ruoyi.system.domain.zbg.WorkOrder;
import com.ruoyi.system.mapper.MaintenanceStaffDataMapper;
import com.ruoyi.system.service.MaintenanceStaffDataService;
import com.ruoyi.system.service.WorkOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class AsyncService {

    @Autowired
    private WorkOrderService workOrderService;
    @Autowired
    private MaintenanceStaffDataMapper maintenanceStaffDataMapper;

    @Async("taskExecutor")
    public void generateWorkOrderAsync(List<WarningAndExceptionData> dataList) {
        log.info("开始生成工单流转");
        for (WarningAndExceptionData data : dataList) {
            WorkOrder workOrder = new WorkOrder();
            //获取审批流的相关数据
            //判断费用类型，根据费用类型生成对应的审批流
            String costCategory = data.getCostCategory();
            //获取到县级市名称
            String district = data.getDistrict();
            //查询改县级市所有的维护人员信息
            QueryWrapper<MaintenanceStaffData> maintenanceStaffDataQueryWrapper = new QueryWrapper<>();
            maintenanceStaffDataQueryWrapper.eq("district", district);
            List<MaintenanceStaffData> maintenanceStaffData = maintenanceStaffDataMapper.selectList(maintenanceStaffDataQueryWrapper);
            //获取第一条数据
            MaintenanceStaffData maintenanceStaff = maintenanceStaffData.get(0);
            workOrder.setWarExcpId(data.getId());
            workOrder.setDistrict(district);

            if (StringUtils.isNotEmpty(costCategory)){
                //查询的不是维修费，只需要查寻塔费，电费，
                if (costCategory.equals("电费")){
                    //获取改工单流水号
                    workOrder.setWorkOrderNumber(data.getWorkOrderNumber());
                    //获取维护人员电话
                    workOrder.setMaintainerPhone(maintenanceStaff.getElectricContact());
                    //获取当前时间localdatetime为创建时间
                    LocalDateTime now = LocalDateTime.now();
                    //更新时间为当前时间
                    workOrder.setCreateTime(now);
                    //设置更新时间为当前时间
                    workOrder.setUpdateTime(now);
                    //设置费用类别为电费
                    workOrder.setCostCategory(costCategory);

                }
                //创建的是塔费
                if (costCategory.equals("塔费")){
                    //获取改工单流水号
                    workOrder.setWorkOrderNumber(data.getWorkOrderNumber());
                    //获取维护人员电话
                    workOrder.setMaintainerPhone(maintenanceStaff.getTowerContact());
                    //获取当前时间localdatetime为创建时间
                    LocalDateTime now = LocalDateTime.now();
                    //更新时间为当前时间
                    workOrder.setCreateTime(now);
                    //设置更新时间为当前时间
                    workOrder.setUpdateTime(now);
                    //设置费用类别为塔费
                    workOrder.setCostCategory(costCategory);
                }
                //创建的是维修费
                if (costCategory.equals("维修费")) {
                    //费用专业是家客
                    if (StringUtils.isNotEmpty(data.getProfession())) {
                        if (data.getProfession().equals("家客")){
                            //获取改工单流水号
                            workOrder.setWorkOrderNumber(data.getWorkOrderNumber());
                            //获取家客维护人员电话
                            workOrder.setMaintainerPhone(maintenanceStaff.getRepairHomeContact());
                            //获取当前时间localdatetime为创建时间
                            LocalDateTime now = LocalDateTime.now();
                            //更新时间为当前时间
                            workOrder.setCreateTime(now);
                            //设置更新时间为当前时间
                            workOrder.setUpdateTime(now);
                            //设置费用类别为维修费
                            workOrder.setCostCategory(costCategory);
                            //设置专业为家客
                            workOrder.setProfession(data.getProfession());

                        }else if (data.getProfession().equals("集客")){
                            //专业是集客
                            //获取改工单流水号
                            workOrder.setWorkOrderNumber(data.getWorkOrderNumber());
                            //获取集客维护人员电话
                            workOrder.setMaintainerPhone(maintenanceStaff.getRepairGroupContact());
                            //获取当前时间localdatetime为创建时间
                            LocalDateTime now = LocalDateTime.now();
                            //更新时间为当前时间
                            workOrder.setCreateTime(now);
                            //设置更新时间为当前时间
                            workOrder.setUpdateTime(now);
                            //设置费用类别为维修费
                            workOrder.setCostCategory(costCategory);
                            //设置专业为集客
                            workOrder.setProfession(data.getProfession());
                        }else if (data.getProfession().equals("动力")){
                            //专业是动力
                            //获取改工单流水号
                            workOrder.setWorkOrderNumber(data.getWorkOrderNumber());
                            //获取动力维护人员电话
                            workOrder.setMaintainerPhone(maintenanceStaff.getRepairPowerContact());
                            //获取当前时间localdatetime为创建时间
                            LocalDateTime now = LocalDateTime.now();
                            //更新时间为当前时间
                            workOrder.setCreateTime(now);
                            //设置更新时间为当前时间
                            workOrder.setUpdateTime(now);
                            //设置费用类别为维修费
                            workOrder.setCostCategory(costCategory);
                            //设置专业为动力
                            workOrder.setProfession(data.getProfession());
                        }else if (data.getProfession().equals("无线")){
                            //专业是无线
                            //获取改工单流水号
                            workOrder.setWorkOrderNumber(data.getWorkOrderNumber());
                            //获取无线维护人员电话
                            workOrder.setMaintainerPhone(maintenanceStaff.getRepairWirelessContact());
                            //获取当前时间localdatetime为创建时间
                            LocalDateTime now = LocalDateTime.now();
                            //更新时间为当前时间
                            workOrder.setCreateTime(now);
                            //设置更新时间为当前时间
                            workOrder.setUpdateTime(now);
                            //设置费用类别为维修费
                            workOrder.setCostCategory(costCategory);
                            //设置专业为无线
                            workOrder.setProfession(data.getProfession());
                        }else if (data.getProfession().equals("代维")){
                            //专业是代维
                            //获取改工单流水号
                            workOrder.setWorkOrderNumber(data.getWorkOrderNumber());
                            //获取代维维护人员电话
                            workOrder.setMaintainerPhone(maintenanceStaff.getRepairMaintenanceContact());
                            //获取当前时间localdatetime为创建时间
                            LocalDateTime now = LocalDateTime.now();
                            //更新时间为当前时间
                            workOrder.setCreateTime(now);
                            //设置更新时间为当前时间
                            workOrder.setUpdateTime(now);
                            //设置费用类别为维修费
                            workOrder.setCostCategory(costCategory);
                            //设置专业为代维
                            workOrder.setProfession(data.getProfession());
                        }
                    }

                }



            }else{
                throw new RuntimeException("费用类型为空，无法生成工单");
            }


            workOrderService.createWorkOrder(workOrder);
        }
    }
}